using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ntx.GD;
using System.Drawing;
using System.Drawing.Imaging;

namespace TileSetRender
{
    public struct MapTile
    {
        public Int16 tileIndex;
        public Int16 specialIndex;
        public MapTile(Int16 p1, Int16 p2)
        {
            tileIndex = p1;
            specialIndex = p2;
        }
    }
    public class Map
    {
        private Int16 width;
        private Int16 height;
        public List<MapTile> tileArray = new List<MapTile>();
        
        public void Map2(string fileName)
        {
            List<byte> byteArray = new List<byte>();
            tileArray = new List<MapTile>();
            FileStream fs = File.OpenRead(fileName);
            BinaryReader br = new BinaryReader(fs);

            for (int i = 0; i < br.BaseStream.Length; i++)
            {
                byteArray.Add(br.ReadByte());

            }

            br.Close();
            fs.Close();

            width = ConvertBytePair(byteArray[0], byteArray[1]);
            height = ConvertBytePair(byteArray[2], byteArray[3]);

            for (int j = 4; j < byteArray.Count; j += 4)
            {
                    tileArray.Add(new MapTile(ConvertBytePair(byteArray[j], byteArray[j + 1]),
                        ConvertBytePair(byteArray[j + 2], byteArray[j + 3])));
                
                /*if((j-4 + 1)%width==0)
                {
                tileArray.Add(new MapTile(49,0));
                tileArray.Add(new MapTile(49,0));
                tileArray.Add(new MapTile(49,0));
                tileArray.Add(new MapTile(49,0));
                }*/
            }
            //width += 4;
        }
        public Map(string fileName)
        {
            List<byte> byteArray = new List<byte>();
            tileArray = new List<MapTile>();
            FileStream fs = File.OpenRead(fileName);
            BinaryReader br = new BinaryReader(fs);
            List<Int16> Intarray = new List<Int16>();
            for (int i = 0; i < br.BaseStream.Length / 2; i++)
            {
                Intarray.Add(br.ReadInt16());                
            }

            br.Close();
            fs.Close();

            width = Intarray[0];
            height = Intarray[1];

            for (int j = 2; j < Intarray.Count; j += 2)
            {
                
                tileArray.Add(new MapTile(Intarray[j],Intarray[j+1]));
                
            }
        }
        private Int16 ConvertBytePair(byte byte1, byte byte2)
        {
            return Convert.ToInt16(byte2.ToString() + byte1.ToString());
        }
        public Int16 Width
        {
            get { return width; }
        }
        public Int16 Height
        {
            get { return height; }
        }
        public List<MapTile> TileArray
        {
            get { return tileArray; }
            set { tileArray = value; }
        }
        public int tileCount()
        {
            return tileArray.Count;
        }
        public static void RenderToFileOld(Map map, R16Parser r16Parser, string filename)
        {
            GD image = new GD(map.Width, map.Height, true);
            //image.Copy(

            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    GD tileTexture = R16Parser.ReturnGDTexture(r16Parser.Tiles[map.TileArray[y * map.Width + x].tileIndex].texture);
                    image.Copy(tileTexture,x * 32, y * 32,0,0,32,32);                

                }
            }
            image.Save(GD.FileType.Png, filename);
        }
        public static void RenderToFile(Map map, R16Parser r16Parser, string filename)
        {
            
            Bitmap canvas = new Bitmap(map.Width * 32, map.Height * 32);
            
            Graphics graphics = Graphics.FromImage(canvas);
            
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    
                    Image tileImage = R16Parser.ReturnImage(r16Parser.Tiles[map.TileArray[y * map.Width + x].tileIndex].texture);
                    
                    Brush brush = new TextureBrush(tileImage);
                    graphics.FillRectangle(brush, x * tileImage.Width, y * tileImage.Height, tileImage.Width, tileImage.Height);
                    
                }
            }
            Bitmap finalBitmap = new Bitmap(map.Width * 32, map.Height * 32, graphics);

            canvas.Save(filename, ImageFormat.Jpeg);

        }
    }

}
